<template>
  <el-dialog
    v-model="visible"
    v-if="load"
    title="复制产品选择"
    width="600"
    draggable
    @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :z-index="2000"
  >
    <template #default>
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="90"
        label-position="left"
      >
        <el-form-item label="SPU信息">
          <el-checkbox v-model="form.spu_cn_title">SPU名称</el-checkbox>
        </el-form-item>
        <el-form-item label="图片">
          <el-checkbox v-model="form.sku_picture">SKU图片</el-checkbox>
        </el-form-item>
        <el-form-item label="推荐">
          <el-checkbox v-model="form.platform_site_info">推荐平台/站点</el-checkbox>
        </el-form-item>
        <el-form-item label="综合信息">
          <!-- <el-checkbox v-model="form.supplier_info">供应商信息</el-checkbox> -->
          <el-checkbox v-model="form.ingredient_info">关联辅料</el-checkbox>
        </el-form-item>
        <el-form-item label="供应商信息">
          <el-checkbox v-model="form.supplier_info">供应商信息</el-checkbox>
          <el-checkbox v-model="form.goods_sku_customs">报关信息</el-checkbox>
          <el-checkbox v-model="form.goods_sku_trade_agent">欧英代产品资料</el-checkbox>
          <el-checkbox v-model="form.goods_sku_identification_processing"
            >产品加工标识</el-checkbox
          >
          <el-checkbox v-model="form.declare_elements_list">申报要素</el-checkbox>
        </el-form-item>
        <el-form-item label="备注">
          <el-checkbox v-model="form.dev_remark">开发备注</el-checkbox>
          <el-checkbox v-model="form.edit_remark">编辑备注</el-checkbox>
          <el-checkbox v-model="form.purchase_remark">采购收货备注</el-checkbox>
          <el-checkbox v-model="form.packaging_remark">仓库打包备注</el-checkbox>
          <el-checkbox v-model="form.inspection_remark">仓库质检备注</el-checkbox>
        </el-form-item>
        <div class="text-[#999999]">注：取消勾选，相关数据不会被复制到新的SKU中</div>
      </el-form>
    </template>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save(formRef)">复制</el-button>
    </template>
  </el-dialog>
</template>
<script setup name="AuditModal">
import { reactive, toRefs, getCurrentInstance } from "vue";
import usePublicStore from "@/store/modules/public";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

const router = useRouter();
const { proxy } = getCurrentInstance();
const emits = defineEmits(["query"]);
const data = reactive({
  form: {},
  visible: false,
  loading: false,
  formRef: null,
  item: null,
  load: false,
});
const { rules, form, visible, loading, formRef, item, load } = toRefs(data);
const open = async (data) => {
  if (!load.value) {
    load.value = true;
    await 1;
  }
  if (data) item.value = data;
  form.value = initForm();
  visible.value = true;
};

const initForm = () => {
  return {
    sku_picture: true,
    supplier_info: true,
    ingredient_info: true,
    dev_remark: true,
    edit_remark: true,
    purchase_remark: true,
    packaging_remark: true,
    inspection_remark: true,
    spu_cn_title: true,
    goods_sku_customs: true,
    goods_sku_trade_agent: true,
    goods_sku_identification_processing: true,
    declare_elements_list: true,
    platform_site_info: true,
  };
};

const save = async () => {
  if (
    !form.value.supplier_info &&
    (form.value.goods_sku_customs ||
      form.value.goods_sku_trade_agent ||
      form.value.goods_sku_identification_processing ||
      form.value.declare_elements_list)
  ) {
    return ElMessage.error(
      "请勾选供应商信息，报关/欧英代产品信息/加工说明均为供应商+SKU维度；若不勾选，无法复制"
    );
  }
  try {
    usePublicStore().setCopyCondition(form.value);
    await router.push({
      path: "/task/product/add",
      query: {
        spuId: item.value.spu_id,
        sku_id: item.value.id,
        type: "copy",
      },
    });
  } catch (error) {
    console.error("Error during navigation:", error);
  } finally {
    close();
  }
};
const close = () => {
  visible.value = false;
};
defineExpose({ open });
</script>
